package com.ruoyi.project.oms.paeaRiskItem.controller;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.sql.Date;
import java.util.*;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.exception.BusinessException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExportUtils;
import com.ruoyi.common.utils.security.ShiroUtils;
import com.ruoyi.project.er.domain.PaeaErError;
import com.ruoyi.project.oms.paeaAuAuditFifldInfo.domain.PaeaAuAuditFifldInfo;
import com.ruoyi.project.oms.paeaAuAuditFifldInfo.service.IPaeaAuAuditFifldInfoService;
import com.ruoyi.project.oms.paeaAuType.domain.PaeaAuType;
import com.ruoyi.project.oms.paeaAuType.service.IPaeaAuTypeService;
import com.ruoyi.project.oms.paeaRiskItem.domain.PaeaRiskItemNumModel;
import com.ruoyi.project.oms.paeaRiskItem.domain.PaeaRiskItemQuery;
import com.ruoyi.project.oms.paeaRiskItemDetail.domain.PaeaRiskItemDetail;
import com.ruoyi.project.paea.paeaDsTrade.domain.PaeaDsTrade;
import com.ruoyi.project.system.user.domain.User;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.oms.paeaRiskItem.domain.PaeaRiskItem;
import com.ruoyi.project.oms.paeaRiskItem.service.IPaeaRiskItemService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

import javax.servlet.http.HttpServletResponse;

/**
 * 预警Controller
 *
 * @author gang.wei
 * @date 2020-03-22
 */
@Controller
@RequestMapping("/oms/paeaRiskItem")
public class PaeaRiskItemController extends BaseController
{
    private String prefix = "oms/paeaRiskItem";

    @Autowired
    private IPaeaRiskItemService paeaRiskItemService;
    @Autowired
    private IPaeaAuTypeService paeaAuTypeService;
    @Autowired
    private IPaeaAuAuditFifldInfoService paeaAuAuditFifldInfoService;
    @RequiresPermissions("oms:paeaRiskItem:view")
    @GetMapping()
    public String riskWelcome()
    {
        return prefix + "/riskWelcome";
    }

    @RequiresPermissions("oms:paeaRiskItem:view")
    @GetMapping("/paeaRiskItem")
    public String paeaRiskItem(ModelMap mmap)
    {
        paeaRiskItemService.init(mmap);
        return prefix + "/paeaRiskItem";
    }

    @RequiresPermissions("oms:paeaRiskItem:view")
    @GetMapping("/paeaRiskItemModel")
    public String paeaRiskItemModel(ModelMap mmap)
    {
        paeaRiskItemService.init(mmap);
        return prefix + "/paeaRiskItemModel";
    }
    /**
     * 查询预警列表
     */
    @RequiresPermissions("oms:paeaRiskItem:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(PaeaRiskItem paeaRiskItem,ModelMap mmap)
    {
        List<PaeaAuType> paeaAuTypes = paeaAuTypeService.queryRiskType();
        mmap.put("paeaAuTypes",paeaAuTypes);
        return getDataTable(new ArrayList<PaeaRiskItem>());
    }

    /**
     * 查询预警表头列表
     */
    @RequiresPermissions("oms:paeaRiskItem:getAuditFifldInfo")
    @PostMapping("/getAuditFifldInfo")
    @ResponseBody
    public AjaxResult getAuditFifldInfo(String impType)
    {
        return AjaxResult.success(paeaRiskItemService.getAuditFifldInfoJson(impType));
    }
    /**
     * 查询预警详情表头列表
     */
    @RequiresPermissions("oms:paeaRiskItem:getAuditDetailFifldInfo")
    @PostMapping("/getAuditDetailFifldInfo")
    @ResponseBody
    public AjaxResult getAuditDetailFifldInfo(String impType)
    {
        return AjaxResult.success(paeaRiskItemService.getAuditDetailFifldInfoJson(impType));
    }
    /**
     * 查询预警表头列表
     */
    @RequiresPermissions("oms:paeaRiskItem:getPaeaRiskItemNumModel")
    @PostMapping("/getPaeaRiskItemNumModel")
    @ResponseBody
    public AjaxResult getPaeaRiskItemNumModel(PaeaRiskItemQuery paeaRiskItemQuery)
    {
        return AjaxResult.success(paeaRiskItemService.getPaeaRiskItemNumModelJson(paeaRiskItemQuery));
    }
    /**
     * 查询预警表头列表
     */
    @RequiresPermissions("oms:paeaRiskItem:getRegisteErrInfo")
    @PostMapping("/getRegisteErrInfo")
    @ResponseBody
    public AjaxResult getRegisteErrInfo(String riskId)
    {
        return paeaRiskItemService.getRegisteErrInfo(riskId);
    }

    /**
     * 创建差错
     */
    @RequiresPermissions("oms:paeaRiskItem:registerPaeaErError")
    @GetMapping("/registerPaeaErError")
    public String registerPaeaErError(PaeaErError paeaErError,ModelMap mmap)
    {
        paeaErError.setCorrectLimitDate(new java.sql.Date(System.currentTimeMillis()));
        mmap.put("paeaErError",paeaErError);
        if(StringUtils.isEmpty(paeaErError.getTradeId())&& StringUtils.isEmpty(paeaErError.getVchNo())){
            //return "er/paeaErErrorRegister/registerErrorWithOutTrade";
            return prefix+"/registerErrorWithOutTrade";
        }
        //return "er/paeaErErrorRegister/registerErrorWithTrade";
        return prefix+"/registerErrorWithTrade";
    }
    /**
     * 查询流水
     */
    @RequiresPermissions("oms:paeaRiskItem:paeaDsTrade")
    @GetMapping("/paeaDsTrade")
    public String paeaDsTrade(PaeaDsTrade paeaDsTrade, ModelMap mmap)
    {
        mmap.put("paeaDsTrade",paeaDsTrade);
        //return "/paea/paeaDsTrade/paeaDsTrade";
        return prefix+"/paeaDsTrade";
    }
    @RequiresPermissions("oms:paeaRiskItem:queryRisk")
    @PostMapping("/queryRisk")
    @ResponseBody
    public TableDataInfo queryRisk(PaeaRiskItemQuery paeaRiskItemQuery)
    {
        //获取表头
        String impType = paeaRiskItemQuery.getImpType()==null?"":paeaRiskItemQuery.getImpType();
        List<PaeaAuAuditFifldInfo> fieldInfos = paeaRiskItemService.getAuAuditFifldInfo(impType);
        //小组
        User user = ShiroUtils.getSysUser();
        String groupCode = paeaRiskItemService.isByGroup(user);
        startPage();
        List<PaeaRiskItem> list = paeaRiskItemService.queryRiskItem(paeaRiskItemQuery,fieldInfos,groupCode);
        return getDataTable(list);
    }

    /**
     * 查询预警详情
     */
    @RequiresPermissions("oms:paeaRiskItem:queryPaeaRiskItemDetail")
    @PostMapping("/queryPaeaRiskItemDetail")
    @ResponseBody
    public TableDataInfo queryPaeaRiskItemDetail(PaeaRiskItemDetail paeaRiskItemDetail)
    {
        startPage();
        List<PaeaRiskItemDetail> list = paeaRiskItemService.queryPaeaRiskItemDetail(paeaRiskItemDetail);
        return getDataTable(list);
    }

    @RequiresPermissions("oms:paeaRiskItem:doConfirm")
    @PostMapping("/doConfirm")
    @ResponseBody
    public AjaxResult doConfirm(String id)
    {
        return paeaRiskItemService.doConfirm(id);
    }
    @RequiresPermissions("oms:paeaRiskItem:doConfirmBatch")
    @PostMapping("/doConfirmBatch")
    @ResponseBody
    public AjaxResult doConfirmBatch(PaeaRiskItemQuery paeaRiskItemQuery,String ids)
    {
        return paeaRiskItemService.doConfirmBatch(paeaRiskItemQuery,ids);
    }
    //批量下发差错
    @RequiresPermissions("oms:paeaRiskItem:addRiskBatch")
    @PostMapping("/addRiskBatch")
    @ResponseBody
    public AjaxResult addRiskBatch(PaeaRiskItemQuery paeaRiskItemQuery,String ids,String auditSuggest,String paeaErPoints)
    {
        return paeaRiskItemService.addRiskBatch(paeaRiskItemQuery,ids,auditSuggest,paeaErPoints);
    }
    //单笔下发差错回调更新
    @RequiresPermissions("oms:paeaRiskItem:registerErrorUpItem")
    @PostMapping("/registerErrorUpItem")
    @ResponseBody
    public AjaxResult registerErrorUpItem(String itemId)
    {
        return paeaRiskItemService.registerErrorUpItem(itemId);
    }
    /**
     * 导出预警列表
     */
    @RequiresPermissions("oms:paeaRiskItem:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(PaeaRiskItem paeaRiskItem)
    {
        List<PaeaRiskItem> list = paeaRiskItemService.selectPaeaRiskItemList(paeaRiskItem);
        ExcelUtil<PaeaRiskItem> util = new ExcelUtil<PaeaRiskItem>(PaeaRiskItem.class);
        return util.exportExcel(list, "paeaRiskItem");
    }
    /**
     * 导出预警列表
     */
    @RequiresPermissions("oms:paeaRiskItem:exportItem")
    @PostMapping("/exportItem")
    @ResponseBody
    public AjaxResult exportItem(PaeaRiskItemQuery paeaRiskItemQuery)
    {
        return  AjaxResult.success(paeaRiskItemService.exportItem(paeaRiskItemQuery));
    }
    /**
     * 导出预警列表
     */
    @RequiresPermissions("oms:paeaRiskItem:exportBatch")
    @PostMapping("/exportBatch")
    @ResponseBody
    public AjaxResult exportBatch(PaeaRiskItemQuery paeaRiskItemQuery)
    {
       return  AjaxResult.success(paeaRiskItemService.exportBatch(paeaRiskItemQuery));
    }
    /**
     * 新增预警
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存预警
     */
    @RequiresPermissions("oms:paeaRiskItem:add")
    @Log(title = "预警", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(PaeaRiskItem paeaRiskItem)
    {
        return toAjax(paeaRiskItemService.insertPaeaRiskItem(paeaRiskItem));
    }

    /**
     * 修改预警
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        PaeaRiskItem paeaRiskItem = paeaRiskItemService.selectPaeaRiskItemById(id);
        mmap.put("paeaRiskItem", paeaRiskItem);
        return prefix + "/edit";
    }

    /**
     * 修改保存预警
     */
    @RequiresPermissions("oms:paeaRiskItem:edit")
    @Log(title = "预警", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(PaeaRiskItem paeaRiskItem)
    {
        return toAjax(paeaRiskItemService.updatePaeaRiskItem(paeaRiskItem));
    }

    /**
     * 删除预警
     */
    @RequiresPermissions("oms:paeaRiskItem:remove")
    @Log(title = "预警", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(paeaRiskItemService.removeByIds(Arrays.asList(ids.split(","))));
    }
}
